#include "video.h"
#include "ui_video.h"


video::video(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::video)
{
    ui->setupUi(this);
    player = new QMediaPlayer(this);
    videoWidget = new QVideoWidget(this);
    Filedialog = new QFileDialog;

    player->setVideoOutput(videoWidget);
    ui->verticalLayout->addWidget(videoWidget);
    medialist =new QMediaPlaylist;
    medialist->addMedia(QUrl::fromLocalFile("D:/video/3.mp4"));
    medialist->addMedia(QUrl::fromLocalFile("D:/video/1.mp4"));
    medialist->addMedia(QUrl::fromLocalFile("D:/video/2.mp4"));
    player->setPlaylist(medialist);

    ui->start_btn->setStyleSheet("min-width: 40px; min-height: 40px; "
                                 "max-width: 40px; max-height: 40px; "
                                 "icon-size: 40px 40px;"
                                 "QPushButton{border-radius:25px;}"
                                 );
    ui->pause_btn->setStyleSheet("min-width: 40px; min-height: 40px; "
                                 "max-width: 40px; max-height: 40px; "
                                 "icon-size: 40px 40px;");
    ui->close_btn->setStyleSheet("min-width: 40px; min-height: 40px; "
                                 "max-width: 40px; max-height: 40px; "
                                 "icon-size: 40px 40px;");
    ui->insert_btn->setStyleSheet("min-width: 40px; min-height: 40px; "
                                 "max-width: 40px; max-height: 40px; "
                                 "icon-size: 40px 40px;");
//    ui->start_btn->setMinimumSize(40,40);
//    ui->start_btn->setMaximumSize(40,40);
//    ui->start_btn->setIconSize(QSize(34,34));
//    ui->start_btn->setText(QStringLiteral("播放"));
    ui->start_btn->setIcon(QIcon(":/icon/start1.png"));
    ui->pause_btn->setIcon(QIcon(":/icon/pause1.png"));
    ui->close_btn->setIcon(QIcon(":/icon/stop1.png"));
    ui->insert_btn->setIcon(QIcon(":/icon/open1.png"));

    connect(ui->start_btn,SIGNAL(clicked()),this,SLOT(starvideo()));
    connect(ui->pause_btn,SIGNAL(clicked()),this,SLOT(pausevideo()));
    connect(ui->close_btn,SIGNAL(clicked()),this,SLOT(closevideo()));
//    connect(ui->resume_btn,SIGNAL(clicked()),this,SLOT(resumevideo()));
    connect(ui->insert_btn,SIGNAL(clicked()),this,SLOT(insertvideo()));
    connect(player, &QMediaPlayer::durationChanged, this, & video::updateDuration);
    connect(player, &QMediaPlayer::positionChanged, this, & video::updatePosition);



}
//
void video::starvideo()
{
    int currentPosition = player->position();
    if(currentPosition==0)
    {
        medialist->setCurrentIndex(0);
        player->play();
    }
    else
    {
        player->setPosition(currentPosition);
        player->play();
    }

}
//暂停播放
void video::pausevideo()
{

    player->pause();
}
//恢复进度播放
//void video::resumevideo()
//{
//    int currentPosition = player->position();

//}
//关闭视频
void video::closevideo()
{
    player->stop();
}
//导入视频
void video::insertvideo()
{
    QString filename;
    filename=Filedialog->getOpenFileName(nullptr,"选择视频文件");
    if(filename != "")
    {
        filename = "rtsp://192.168.241.1:554/live";
        QUrl videoUrl(filename);
        player->setMedia(videoUrl);
        player->play();

    }

}
void video::setVideoUrl(QString url) {
    if(url != "")
    {
        QUrl videoUrl(url);
        player->setMedia(videoUrl);
        player->play();
    }
}

void video::closeEvent(QCloseEvent *event) {
    closevideo();
    this->hide();
}

void video::updateDuration(qint64 m_duration)
{
    int sec=m_duration/1000;
    int min=sec/60;
    sec=sec%60;
    QString totaltime=QString::number(min)+':'+QString::number(sec);
    ui->total_time->setText(totaltime);
    qDebug()<<totaltime;

}
void video::updatePosition(qint64 m_currentPosition)
{
    if(ui->video_time->isSliderDown())
        return;
    float a=float(m_currentPosition);
    float b=float(player->duration());
    ui->video_time->setSliderPosition((a/b)*100);
    int sec = m_currentPosition/1000;
    int min = sec/60;
    sec = sec%60;
    QString current_time=QString::number(min)+':'+QString::number(sec);
    ui->current_time->setText(current_time);
    qDebug()<<current_time;
}
void video::on_video_time_valueChanged(int value)
{
    if(ui->video_time->isSliderDown()==true)
    {
        player->setPosition((value*player->duration())/100.0);
    }
}
//QString video::get_video_info(QString filename)
//{
//    return filename;
//}
video::~video()
{

    delete ui;
}
